1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  package org.hibernate.ejb.test.cascade.multicircle;
25  
26  import javax.persistence.EntityManager;
27  import javax.persistence.RollbackException;
28  
29  import junit.framework.Assert;
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  import org.hibernate.TransientPropertyValueException;
35  import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
36  import org.hibernate.testing.FailureExpected;
37  
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.assertTrue;
40  import static org.junit.Assert.fail;
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  public class MultiCircleJpaCascadeTest extends BaseEntityManagerFunctionalTestCase {
91  	private B b;
92  	private C c;
93  	private D d;
94  	private E e;
95  	private F f;
96  	private G g;
97  	private boolean skipCleanup;
98  
99  	@Before
100 	public void setup() {
101 		b = new B();
102 		c = new C();
103 		d = new D();
104 		e = new E();
105 		f = new F();
106 		g = new G();
107 
108 		b.getGCollection().add( g );
109 		b.setC( c );
110 		b.setD( d );
111 
112 		c.getBCollection().add( b );
113 		c.getDCollection().add( d );
114 
115 		d.getBCollection().add( b );
116 		d.setC( c );
117 		d.setE( e );
118 		d.getFCollection().add( f );
119 
120 		e.getDCollection().add( d );
121 		e.setF( f );
122 
123 		f.getECollection().add( e );
124 		f.setD( d );
125 		f.setG( g );
126 
127 		g.setB( b );
128 		g.getFCollection().add( f );
129 
130 		skipCleanup = false;
131 	}
132 
133 	@After
134 	public void cleanup() {
135 		if ( ! skipCleanup ) {
136 			b.setC( null );
137 			b.setD( null );
138 			b.getGCollection().remove( g );
139 
140 			c.getBCollection().remove( b );
141 			c.getDCollection().remove( d );
142 
143 			d.getBCollection().remove( b );
144 			d.setC( null );
145 			d.setE( null );
146 			d.getFCollection().remove( f );
147 
148 			e.getDCollection().remove( d );
149 			e.setF( null );
150 
151 			f.setD( null );
152 			f.getECollection().remove( e );
153 			f.setG( null );
154 
155 			g.setB( null );
156 			g.getFCollection().remove( f );
157 
158 			EntityManager em = getOrCreateEntityManager();
159 			em.getTransaction().begin();
160 			b = em.merge( b );
161 			c = em.merge( c );
162 			d = em.merge( d );
163 			e = em.merge( e );
164 			f = em.merge( f );
165 			g = em.merge( g );
166 			em.remove( f );
167 			em.remove( g );
168 			em.remove( b );
169 			em.remove( d );
170 			em.remove( e );
171 			em.remove( c );
172 			em.getTransaction().commit();
173 			em.close();
174 		}
175 	}
176 
177 	@Test
178 	public void testPersist() {
179 		EntityManager em = getOrCreateEntityManager();
180 		em.getTransaction().begin();
181 		em.persist( b );
182 		em.getTransaction().commit();
183 		em.close();
184 
185 		check();
186 	}
187 
188 	@Test
189 	public void testPersistNoCascadeToTransient() {
190 		skipCleanup = true;
191 		EntityManager em = getOrCreateEntityManager();
192 		em.getTransaction().begin();
193 		try {
194 			em.persist( c );
195 			fail( "should have failed." );
196 		}
197 		catch( IllegalStateException ex ) {
198 			assertTrue( TransientPropertyValueException.class.isInstance( ex.getCause() ) );
199 			TransientPropertyValueException pve = (TransientPropertyValueException) ex.getCause();
200 			assertEquals( G.class.getName(), pve.getTransientEntityName() );
201 			assertEquals( F.class.getName(),  pve.getPropertyOwnerEntityName() );
202 			assertEquals( "g", pve.getPropertyName() );
203 		}
204 		em.getTransaction().rollback();
205 		em.close();
206 	}
207 
208 	@Test
209 	@FailureExpected( jiraKey = "HHH-6999" )
210 	
211 	public void testPersistThenUpdate() {
212 		EntityManager em = getOrCreateEntityManager();
213 		em.getTransaction().begin();
214 		em.persist( b );
215 		
216 		e.getDCollection().remove( d );
217 		d.setE( null );
218 		f.getECollection().remove( e );
219 		e.setF( null );
220 		
221 		e = new E();
222 		e.getDCollection().add( d );
223 		f.getECollection().add( e );
224 		d.setE( e );
225 		e.setF( f );
226 		em.getTransaction().commit();
227 		em.close();
228 
229 		check();
230 	}
231 
232 	@Test
233 	public void testPersistThenUpdateNoCascadeToTransient() {
234 		
235 		skipCleanup = true;
236 
237 		
238 		c.getBCollection().clear();
239 		c.getDCollection().clear();
240 
241 		EntityManager em = getOrCreateEntityManager();
242 		em.getTransaction().begin();
243 		em.persist( c );
244 		
245 		c.getBCollection().add( b );
246 		c.getDCollection().add( d );
247 		try {
248 			em.getTransaction().commit();
249 			fail( "should have thrown IllegalStateException");
250 		}
251 		catch ( RollbackException ex ) {
252 			assertTrue( ex.getCause() instanceof IllegalStateException );
253 			IllegalStateException ise = ( IllegalStateException ) ex.getCause();
254 			
255 			
256 			
257 			
258 			
259 			assertTrue( ise.getCause() instanceof TransientPropertyValueException );
260 			TransientPropertyValueException tpve = ( TransientPropertyValueException ) ise.getCause();
261 			assertEquals( E.class.getName(), tpve.getTransientEntityName() );
262 			assertEquals( D.class.getName(), tpve.getPropertyOwnerEntityName() );
263 			assertEquals( "e", tpve.getPropertyName() );
264 		}
265 		em.close();
266 	}
267 
268 	@Test
269 	public void testMerge() {
270 		EntityManager em = getOrCreateEntityManager();
271 		em.getTransaction().begin();
272 		b = em.merge( b );
273 		c = b.getC();
274 		d = b.getD();
275 		e = d.getE();
276 		f = e.getF();
277 		g = f.getG();
278 		em.getTransaction().commit();
279 		em.close();
280 
281 		check();
282 	}
283 
284 	private void check() {
285 		EntityManager em = getOrCreateEntityManager();
286 		em.getTransaction().begin();
287 		B bRead = em.find( B.class, b.getId() );
288 		Assert.assertEquals( b, bRead );
289 
290 		G gRead = bRead.getGCollection().iterator().next();
291 		Assert.assertEquals( g, gRead );
292 		C cRead = bRead.getC();
293 		Assert.assertEquals( c, cRead );
294 		D dRead = bRead.getD();
295 		Assert.assertEquals( d, dRead );
296 
297 		Assert.assertSame( bRead, cRead.getBCollection().iterator().next() );
298 		Assert.assertSame( dRead, cRead.getDCollection().iterator().next() );
299 
300 		Assert.assertSame( bRead, dRead.getBCollection().iterator().next() );
301 		Assert.assertEquals( cRead, dRead.getC() );
302 		E eRead = dRead.getE();
303 		Assert.assertEquals( e, eRead );
304 		F fRead = dRead.getFCollection().iterator().next();
305 		Assert.assertEquals( f, fRead );
306 
307 		Assert.assertSame( dRead, eRead.getDCollection().iterator().next() );
308 		Assert.assertSame( fRead, eRead.getF() );
309 
310 		Assert.assertSame( eRead, fRead.getECollection().iterator().next() );
311 		Assert.assertSame( dRead, fRead.getD() );
312 		Assert.assertSame( gRead, fRead.getG());
313 
314 		Assert.assertSame( bRead, gRead.getB() );
315 		Assert.assertSame( fRead, gRead.getFCollection().iterator().next() );
316 
317 		em.getTransaction().commit();
318 		em.close();
319 	}
320 
321 	@Override
322 	protected Class[] getAnnotatedClasses() {
323 		return new Class[]{
324 				B.class,
325 				C.class,
326 				D.class,
327 				E.class,
328 				F.class,
329 				G.class
330 		};
331 	}
332 
333 }